package com.gillion.grule.client.core.variable;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import java.util.Map;
import java.util.Objects;
import java.util.Set;


public class PackageVariable extends BaseVariable
{
    private GlobalVariable globalVariable;
    private Map<String, AssemblyVariable> assemblyVariables = Maps.newConcurrentMap();
    private Map<String, DecisionVariable> decisionVariables = Maps.newConcurrentMap();
    private Map<String, DecisionTreeVariable> decisionTreeVariables = Maps.newConcurrentMap();
    public  Map<String, DecisionTreeVariable> getDecisionTreeVariables() {
        return this.decisionTreeVariables;
    }
    public void setDecisionTreeVariables(Map<String, DecisionTreeVariable> decisionTreeVariables) {
        this.decisionTreeVariables = decisionTreeVariables;
    }

    @JsonIgnore
    public Object getVariable(String name) {
        Object t = this.variables.get(name);
        return (t != null) ? t : this.globalVariable.getVariable(name);
    }


    public void init() {
        super.init();
        Set<String> keys = getAssemblyVariables().keySet();
        for (String key : keys) {
            ((AssemblyVariable)getAssemblyVariables().get(key)).init();
        }
    }







    @JsonIgnore
    public Object getVariable(String... args) {
        Object t = null;
        switch (args.length) {

            case 1:
                return getVariable(args[0]);
            case 2:
                return ((AssemblyVariable)
                        getAssemblyVariables()
                                .get(args[0]))
                        .getVariable(args[1]);
            case 3:
                return ((RuleVariable)((AssemblyVariable)
                        getAssemblyVariables()
                                .get(args[0]))
                        .getRuleVariables()
                        .get(args[1]))
                        .getVariable(args[2]);
        }
        return t;
    }




    public String toString() {
        return "PackageVariable{}";
    }


    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof PackageVariable)) {
            return false;
        }
        if (!super.equals(o)) {
            return false;
        }

        PackageVariable that = (PackageVariable)o;
        return
                Objects.equals(this.globalVariable, that.globalVariable) &&
                Objects.equals(this.assemblyVariables, that.assemblyVariables);
    }


    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + ((this.globalVariable != null) ? this.globalVariable.hashCode() : 0);
        result = 31 * result + ((this.assemblyVariables != null) ? this.assemblyVariables.hashCode() : 0);
        return result;
    }

    public GlobalVariable getGlobalVariable() {
        return this.globalVariable;
    }

    public void setGlobalVariable(GlobalVariable globalVariable) {
        this.globalVariable = globalVariable;
    }

    public Map<String, AssemblyVariable> getAssemblyVariables() {
        return this.assemblyVariables;
    }

    public void setAssemblyVariables(Map<String, AssemblyVariable> assemblyVariables) {
        this.assemblyVariables = assemblyVariables;
    }

    public Map<String, DecisionVariable> getDecisionVariables() {
        return this.decisionVariables;
    }

    public void setDecisionVariables(Map<String, DecisionVariable> decisionVariables) {
        this.decisionVariables = decisionVariables;
    }
}